function BeginEating( pl, ent, eattime, eattimeleft )

	if !ent:GetNWBool("IsBeingEaten") then
		ent:SetNWBool("IsBeingEaten", true)
		umsg.Start("begineating", pl)
		umsg.Entity( ent )
		umsg.End()
		pl:SetNWBool("IsEating", true)
		pl:SetNWEntity( "CurFood", ent )
		
		if pl:HasWeapon("fork") then eattime = math.Clamp(eattime - 2, 1, 20) end
		pl:Spectate( MODE_CHASE )
		local mdlview = ents.Create( "prop_dynamic" )
		mdlview:SetColor( 0, 0, 0, 0 )
		mdlview:SetModel( "models/props_junk/watermelon01.mdl" )
		mdlview:SetAngles( pl:GetAngles( ) )
		mdlview:SetPos( pl:GetPos( ) + Vector( 0, 0, 60 ))-- + pl:GetForward()*-128 )
		mdlview:SetMoveType( MOVETYPE_NONE )
		mdlview:SetRenderMode( RENDERMODE_NONE )
		mdlview:SetSolid( SOLID_NONE )
		mdlview:SetParent( pl )
		mdlview:DrawShadow( false )
		mdlview:Spawn( )
		pl:SetViewEntity(mdlview)
		pl:SetNWEntity( "Cam", mdlview )
		timer.Create( tostring(pl:UniqueID() .. "2"), 0.1, 0, WeightGain, pl, ent )
		timer.Create( tostring(pl:UniqueID()), eattimeleft + 0.1, 1, CompleteEating, pl, ent )
		timer.Create( tostring(pl:UniqueID() .. "1"), 0.25, eattimeleft*4, CheckShouldInterruptEating, pl, ent )
	end
	
end

function CheckShouldInterruptEating( pl, ent )

	local trace = pl:GetEyeTrace()
		if trace.Entity != ent or pl:GetPos():Distance(ent:GetPos()) > 128 then
			InterruptEating( pl, nil, ent )
		end
	
end

function WeightGain( pl, ent )

	if pl:GetNWBool("IsEating") then
		local curweight = pl:GetNWInt("weight")
		local addweight = (ent.Weight/ent.EatTime)/10
		local newweight = curweight + addweight
		pl:SetNWInt( "weight", newweight )
		local newspeed = math.Clamp( 450-(3*newweight), 50, 450 )
		GAMEMODE:SetPlayerSpeed( pl, newspeed, newspeed )
		ent:SetDTFloat(0, (ent:GetDTFloat(0) - 0.2))
		pl:SetJumpPower( 350-(newweight*2) )
		local bone = 0 --pl:LookupBone("ValveBiped.Bip01_Pelvis")
		local m = pl:GetBoneMatrix(bone)
		m:Scale(Vector(1, 1, (newweight/100)+1))
		pl:SetBoneMatrix(bone, m)
	end

end

function InterruptEating( victim, attacker, ent )

	if victim:GetNWBool("IsEating") or ent:GetNWBool("IsBeingEaten") then
		if ent.Sticky then return end
		
		umsg.Start("interrupteating", pl)
			if attacker then
				umsg.String( attacker:Nick() )
			end
		umsg.End()
		
		victim:SetNWBool("IsEating", false)
		timer.Destroy(tostring(victim:UniqueID()))
		timer.Destroy(tostring(victim:UniqueID() .. "1"))
		timer.Destroy(tostring(victim:UniqueID() .. "2"))
		ent:SetNWBool("IsBeingEaten", false)
		victim:UnSpectate()
		victim:SetViewEntity(victim)
		if victim:GetNWEntity( "Cam" ) then victim:GetNWEntity( "Cam" ):Remove() end
	end

end

function CompleteEating( pl, ent )

	local trace = pl:GetEyeTrace()
	if trace.Entity == ent and pl:GetPos():Distance(trace.HitPos) < 108 then
		ent:SetNWBool("IsBeingEaten", false)
		umsg.Start("completeeating", pl)
		umsg.Long( ent.Weight )
		umsg.End()
		if ent.Swep then
			pl:Give(ent.Swep)
		end
		if ent.Special then
			ent:GetTable():DoSpecial( pl )
		end
		ent:Remove()
		if math.Clamp(pl:GetNWInt("weight") - math.random(60), 0, 100) > 50 and pl:GetNWInt("weight") < 100 then
			HeartAttack( pl )
		else
			pl:UnSpectate()
			pl:SetViewEntity(pl)
			if pl:GetNWEntity( "Cam" ) then pl:GetNWEntity( "Cam" ):Remove() end
		end
		pl:SetNWBool("IsEating", false)
	end
	
end

function HeartAttack( pl )

	pl:ChatPrint("You're having a heart attack, mash movement keys to get up!")
	pl:SetNWBool("IsDying", true)
	pl:SetNWInt("ButtonsLeft", 20)
	GAMEMODE:SetPlayerSpeed( pl, 0, 0 )
	pl:SetJumpPower( 0 )
	pl:Spectate()
	pl:SetViewEntity(pl:GetNWEntity("Cam"))
	
end
